//
//  Alert.swift
//  JKTools
//
//  Created by 姜奎 on 2021/9/26.
//

import Foundation
import Cocoa

struct Alert {
    
    static func alert(message: String) {
        DispatchQueue.main.async{
            let alert = NSAlert()
            alert.messageText = "🐢 JKTools"
            alert.informativeText = message
            alert.addButton(withTitle: "OK")
            alert.runModal()
        }
    }
    
    static func alert(message: String, buttons:[String], completionHandler: @escaping (NSApplication.ModalResponse) -> Void) {
        DispatchQueue.main.async {
            let alert = NSAlert()
            alert.messageText = "🐢 JKTools"
            alert.informativeText = message
            for button in buttons {
                alert.addButton(withTitle: button)
            }
            
            let modalResponse = alert.runModal()
            completionHandler(modalResponse)
        }
    }

    
    static func alert(message: String, buttons:[String]) async -> NSApplication.ModalResponse {
        return await withCheckedContinuation { continuation in
            alert(message: message, buttons: buttons) { response in
                continuation.resume(returning: response)
            }
        }
    }

}
